<?php


    namespace addons\zpwxsys\controller\v1;

    use addons\zpwxsys\controller\BaseController;
    use addons\zpwxsys\model\Nav as NavModel;
    use addons\zpwxsys\model\City as CityModel;
    use addons\zpwxsys\model\Areainfo as AreaModel;
    use addons\zpwxsys\model\Note as NoteModel;
    use addons\zpwxsys\model\Jobcate as JobcateModel;
    use addons\zpwxsys\model\Jobrecord as JobrecordModel;
    use addons\zpwxsys\model\Job as JobModel;
    use addons\zpwxsys\model\Jobsave as JobsaveModel;
    use addons\zpwxsys\model\Jobprice as JobpriceModel;
    use addons\zpwxsys\model\Sysmsg as SysmsgModel;
    use addons\zpwxsys\model\Askjob as AskjobModel;
    use addons\zpwxsys\model\Worktype as WorktypeModel;
    use addons\zpwxsys\model\Sysinit as SysinitModel;
    use addons\zpwxsys\model\Task as TaskModel;
     use addons\zpwxsys\service\AccessToken as AccessToken;
    use addons\zpwxsys\service\CompanyRecord as CompanyRecordService;
    use addons\zpwxsys\validate\IDMustBePositiveInt;
    use addons\zpwxsys\lib\exception\MissException;
    use addons\zpwxsys\service\Token;
    use think\Db;


    class Job extends BaseController
    {


        public function sendCompanyJob()
            {
                if(request()->isPost()){
                    $param = input('post.');
                    $param['uid'] = Token::getCurrentUid();

                    $taskid = $param['taskid'];

                    $noteid = $param['noteid'];

                    $taskinfo = TaskModel::getTask(array('id'=>$taskid));

                    $umap =[];
                    $umap['id'] = $noteid;
                    $umap['tid'] = $param['uid'];//归属该经纪人

                    $noteinfo =  NoteModel::getNoteMap($umap);

                    if($noteinfo)
                    {
                    $map['jobid'] = $taskinfo['jobid'];
                    $map['companyid'] = $taskinfo['companyid'];
                    $map['uid'] =  $noteinfo['uid'];
                    $mapjob['id'] =    $taskinfo['jobid'] ;
                    $jobinfo = JobModel::getOne($mapjob);

                    $jobrecord =  JobrecordModel::getJobrecordWhere($map);

                    if($jobrecord)
                    {

                        $data = json_encode(array('status'=>2,'msg'=>'您已经投递过'));

                    }else{

                        $param2['createtime'] = time();

                         $param2['jobid'] = $taskinfo['jobid'];
                        $param2['companyid'] = $taskinfo['companyid'];
                        $param2['uid'] =  $noteinfo['uid'];

                        $param2['noteid'] = $noteinfo['id'];

                        $param2['agentuid'] =  $param['uid'] ;
                          $param2['taskid'] =  $taskid ;

                        $jobrecord = new JobrecordModel();



                        $data = $jobrecord->sendJob($param2);




                    }

                    }else{


                         $data = json_encode(array('status'=>3,'msg'=>'请先完善简历'));
                    }


                    return $data;
                }



            }


        public function getJoblist()
        {



            $cityid = input('post.cityid');

            $areaid = input('post.areaid');

            $jobcateid = input('post.jobcateid');

            $jobpriceid = input('post.priceid');


            $type = input('post.type');

            if (!empty($type) && $type > 0) {

                $mapjob['j.type'] = $type;
            }

              if ($cityid > 0) {

                $mapjob['m.cityid'] = $cityid;
            }


            if ($areaid > 0) {

                $mapjob['m.areaid'] = $areaid;
            }


            if ($jobcateid > 0) {

                $mapjob['j.worktype'] = $jobcateid;
            }

            if ($jobpriceid > 0) {

                $mapjob['j.jobpriceid'] = $jobpriceid;
            }


            $cityid = input('post.cityid');

            $odjob = "j.sort desc,j.updatetime  desc";
            $mapjob['j.status'] = 1;
            $mapjob['j.ischeck'] = 1;
            $mapjob['m.status'] = 1;


            $Nowpage = input('post.page');
            if ($Nowpage) $Nowpage = $Nowpage; else
                $Nowpage = 1;

            $limits = $Nowpage * 10;
            $Nowpage = 1;

            $mapjob['j.toptime'] = array('lt', time());

            $JobModel = new JobModel();
            $joblist = $JobModel->getJobByWhere($mapjob, $Nowpage, $limits, $odjob);
            $joblistcount = $JobModel->joblistcount($mapjob, $odjob);
            $mapjob['j.toptime'] = array('gt', time());

            $topjoblist = $JobModel->getJobByWhere($mapjob, $Nowpage, $limits, $odjob);


            $arealist = AreaModel::getAreaByCityid($cityid);

            $jobcatelist = JobcateModel::getJobcatelist();

            $worktype = WorktypeModel::getOne(array('id' => $type));

            $maptype['enabled'] = 1;
            $od = 'sort desc';


            $worktypelist = WorktypeModel::getList($maptype, $od);

            $jobpricelist = JobpriceModel::getJobpricelist();

            $data = array('joblist' => $joblist,'joblistcount' =>$joblistcount, 'arealist' => $arealist, 'jobcatelist' => $jobcatelist, 'topjoblist' => $topjoblist, 'worktype' => $worktype, 'worktypelist' => $worktypelist,'jobpricelist'=>$jobpricelist);


            return json_encode($data);


        }


        public function getInitJob()
        {

            $cityid = input('post.cityid');
            $arealist = AreaModel::getAreaByCityid($cityid);
            $jobcatelist = JobcateModel::getJobcatelist();

            $maptype['enabled'] = 1;
            $od = 'sort desc';
            $worktypelist = WorktypeModel::getList($maptype, $od);
             $data = array('arealist' => $arealist, 'jobcatelist' => $jobcatelist, 'worktypelist' => $worktypelist);


            return json_encode($data);

        }


        public function getNavDetail()
        {
             $id = input('post.id');

            $validate = new IDMustBePositiveInt();
            $validate->goCheck();


            $map = array('id' => $id);

            $navinfo = NavModel::getOne($map);

            if(!$navinfo)
            {
                 $data = json_encode(array('status'=>1,'msg'=>'请求数据不存在'));
                   return json_encode($data);

            }

            $data = array('navinfo' => $navinfo,'status'=>0,'msg'=>'请求数据正常');


            return json_encode($data);


        }


        public function getJobIndexList()
        {

            $cityid = input('post.cityid');
            $worktype = input('post.worktype');


            $od = "j.createtime desc";

            $map = array();

            if ($cityid > 0) {

                $map['m.cityid'] = $cityid;
            }

            if ($worktype >= 0) {

                $map['j.type'] = $worktype;

            }

            $map['m.status'] = 1;
            $map['j.ischeck'] = 1;
            $map['j.status'] = 1;

            $limits = 60;
            $Nowpage = 1;

            $JobModel = new JobModel();
            $joblist = $JobModel->getJobByWhere($map, $Nowpage, $limits, $od);


            $data = array('joblist' => $joblist);


            return json_encode($data);


        }


        public function getmatchjoblist()
        {


            $city = input('post.city');

            $uid = Token::getCurrentUid();
            $noteinfo = NoteModel::getNoteByuid($uid);

            $cityinfo = CityModel::getCityByName($city);


            $jobcateid = $noteinfo['jobcateid'];

            $mapcate['id'] = $jobcateid;

            $jobcate = JobcateModel::getOne($mapcate);


            if ($jobcateid > 0) {

                $mapjob['j.worktype'] = $jobcateid;
            }


            $cityid = input('post.cityid');

            $odjob = "j.updatetime desc";
            $mapjob['j.status'] = 1;
            $mapjob['m.status'] = 1;

            $Nowpage = input('post.page');
            if ($Nowpage) $Nowpage = $Nowpage; else
                $Nowpage = 1;

            $limits = $Nowpage * 10;
            $Nowpage = 1;


            $JobModel = new JobModel();
            $joblist = $JobModel->getJobByWhere($mapjob, $Nowpage, $limits, $odjob);


            $data = array('joblist' => $joblist, 'jobcate' => $jobcate);


            return json_encode($data);


        }


        public function getNearjoblist()
        {


            $city = input('post.city');



            $cityinfo = CityModel::getCityByName($city);

            $areaid = input('post.areaid');

            $jobcateid = input('post.jobcateid');

            $jobpriceid = input('post.priceid');

            $type = input('post.type');

            $latitude = input('post.latitude');

            $longitude = input('post.longitude');


            if (!empty($type) && $type > 0) {

                $mapjob['j.type'] = $type;
            }

            if ($areaid > 0) {

                $mapjob['m.areaid'] = $areaid;
            }


            if ($jobcateid > 0) {

                $mapjob['j.worktype'] = $jobcateid;
            }

            if ($jobpriceid > 0) {

                $mapjob['j.jobpriceid'] = $jobpriceid;
            }


            $cityid = input('post.cityid');

            $odjob = " distance asc ,j.updatetime desc";
            $mapjob['j.status'] = 1;
         //   $mapjob['m.status'] = 0;


            $Nowpage = input('post.page');


            if ($Nowpage) $Nowpage = $Nowpage; else
                $Nowpage = 1;

            $limits = $Nowpage * 10;
            $Nowpage = 1;

            $JobModel = new JobModel();

            $joblist = $JobModel->getNearJobByWhere($mapjob, $Nowpage, $limits, $odjob, $latitude, $longitude);




            $arealist = AreaModel::getAreaByCityid($cityid);

            $jobcatelist = JobcateModel::getJobcatelist();


            $maptype['enabled'] = 1;
            $od = 'sort desc';


            $worktypelist = WorktypeModel::getList($maptype, $od);


            $data = array('joblist' => $joblist, 'arealist' => $arealist, 'jobcatelist' => $jobcatelist, 'worktypelist' => $worktypelist);


            return json_encode($data);


        }


        public function getSearchJobList()
        {

            $cityid = input('post.cityid');
            $cateid = input('post.cateid');
            $priceid = input('post.priceid');
            $edu = input('post.edu');
            $express = input('post.express');
            $sex = input('post.sex');
            $special = input('post.special');


            $od = "j.createtime desc";

            $map = array();

            if ($cityid > 0) {

                $map['m.cityid'] = $cityid;
            }

            if ($cateid > 0) {

                $map['j.worktype'] = $cateid;
            }

            if ($priceid > 0) {

                $map['j.jobpriceid'] = $priceid;
            }


            if ($edu != "") {

                $map['j.education'] = $edu;
            }

            if ($express != "") {

                $map['j.express'] = $express;
            }


            if ($sex != -1) {

                $map['j.sex'] = $sex;
            }


            if ($special != "") {

                $map['j.special'] = array('like', '%' . $special . '%');
            }

           // $map['m.status'] = 0;

            $map['j.status'] = 1;
            $map['j.ischeck'] = 1;


            $limits = 1000000;
            $Nowpage = 1;

            if (count($map) > 0) {
                $JobModel = new JobModel();
                $joblist = $JobModel->getJobByWhere($map, $Nowpage, $limits, $od);
            } else {

                $joblist = [];
            }


            $data = array('joblist' => $joblist);


            return json_encode($data);


        }


        public function getJobListCount()
        {

            $cityid = input('post.cityid');
            $cateid = input('post.cateid');
            $priceid = input('post.priceid');
            $edu = input('post.edu');
            $express = input('post.express');
            $sex = input('post.sex');
            $special = input('post.special');


            $od = "j.createtime desc";

            $map = array();

            if ($cityid > 0) {

                $map['m.cityid'] = $cityid;
            }

            if ($cateid > 0) {

                $map['j.worktype'] = $cateid;
            }

            if ($priceid > 0) {

                $map['j.jobpriceid'] = $priceid;
            }


            if ($edu != "") {

                $map['j.education'] = $edu;
            }

            if ($express != "") {

                $map['j.express'] = $express;
            }


            if ($sex != -1) {

                $map['j.sex'] = $sex;
            }


            if ($special != "") {

                $map['j.special'] = array('like', '%' . $special . '%');
            }

            $map['j.status'] = 1;
            $map['j.ischeck'] = 1;

            $limits = 1000000;
            $Nowpage = 1;

            if (count($map) > 0) {
                $JobModel = new JobModel();
                $joblist = $JobModel->getJobByWhere($map, $Nowpage, $limits, $od);
                $jobcount = count($joblist);
            } else {

                $jobcount = 0;
            }


            $data = array('jobcount' => $jobcount);


            return json_encode($data);


        }

        public function getJobdetail()
        {

            $id = input('get.id');

            $validate = new IDMustBePositiveInt();
            $validate->goCheck();


            $JobModel = new JobModel();

            $map = array('id' => $id);

            $jobinfo = $JobModel->getJob($map)->toArray();

            if (!$jobinfo) {
                return json_encode(['status'=>1,'msg'=>'请求数据不存在']);
            }
            $mapsave = array('jobid' => $jobinfo['id']);
            $jobsave = JobsaveModel::getJobsaveWhere($mapsave);
            if ($jobsave) {

                $jobinfo['savestatus'] = 1;

            } else {

                $jobinfo['savestatus'] = 0;
            }

            $jobinfo['special'] = explode(',', $jobinfo['special']);
            $jobrecord_count = Db::name('zpwxsys_jobrecord')->where(['jobid'=>$id])->count();
            $uid = Db::name('zpwxsys_jobrecord')->where(['jobid'=>$id])->field('uid')->select();
            $jobinfo['jobrecord_user']['count'] = $jobrecord_count;
            $avatarUrl =[];
            foreach ($uid as $key=>$value){
                $zpwxsys_user = Db::name('zpwxsys_user')->where(['id'=>$value['uid']])->find();
                if(is_array($zpwxsys_user))
                {
                    $zpwxsys_user['avatarUrl'] = cdnurl($zpwxsys_user['avatarUrl'], true);
                    if($zpwxsys_user['avatarUrl']!=''){
                        $avatarUrl[] = $zpwxsys_user['avatarUrl'];
                    }
                }

            }
            $jobinfo['jobrecord_user']['avatarUrl'] = $avatarUrl;

            $odjob = "j.updatetime desc";

            $mapjob['j.status'] = 1;
            $mapjob['j.ischeck'] = 1;
            $mapjob['m.status'] = 1;

            $mapjob['j.companyid'] = $jobinfo['companyid'];
            $limits = 10;
            $Nowpage = 1;


            $JobModel = new JobModel();
            $joblist = $JobModel->getJobByWhere($mapjob, $Nowpage, $limits, $odjob);


            $data = array('joblist' => $joblist, 'jobinfo' => $jobinfo,'status'=>0,'msg'=>'请求数据正常');


            return json_encode($data);
        }

        public function sendJob()
        {
            if (request()->isPost()) {

                $param = input('post.');
                $param['uid'] = Token::getCurrentUid();

                $noteinfo = NoteModel::getNoteByuid($param['uid']);



                if ($noteinfo) {


                if($noteinfo['status'] == 0)
                {

                       $data = json_encode(array('status' => 2, 'msg' => '简历禁用,请联系客服'));

                }else{

                    $map['jobid'] = $param['jobid'];
                    $map['companyid'] = $param['companyid'];
                    $map['uid'] = $param['uid'];

                    $mapjob['id'] = $map['jobid'];
                    $jobinfo = JobModel::getOne($mapjob);

                    $jobrecord = JobrecordModel::getJobrecordWhere($map);

                    if ($jobrecord) {

                        $data = json_encode(array('status' => 2, 'msg' => '您已经投递过'));

                    } else {

                        $param['createtime'] = time();
                        $param['agentuid'] = 0 ;
                        $param['taskid'] = 0 ;

                        $jobrecord = new JobrecordModel();

                        $data = $jobrecord->sendJob($param);


                        $msg['uid'] = $param['uid'];
                        $msg['content'] = '恭喜您，投递职位《' . $jobinfo['jobtitle'] . '》成功';
                        $msg['createtime'] = time();

                        $sysmsgModel = new SysmsgModel();

                        $sysmsgModel->insertSysmsg($msg);


                    }

                }

                } else {


                    $data = json_encode(array('status' => 3, 'msg' => '请先完善简历'));
                }




                return $data;
            }


        }


        public function saveAskjob()
        {
            if (request()->isPost()) {

                $param = input('post.');
                $param['uid'] = Token::getCurrentUid();

                $map['jobid'] = $param['jobid'];

                $map['content'] = $param['content'];

                $jmap['id'] = $map['jobid'];

                $jobinfo = JobModel::getOne($jmap);

                $map['companyid'] = $jobinfo['companyid'];

                $map['uid'] = $param['uid'];

                $map['createtime'] = time();

                $askjob = new AskjobModel();

                $data = $askjob->saveAskjob($map);


                return $data;
            }


        }


        public function jobSave()//收藏职位
        {
            if (request()->isPost()) {

                $param = input('post.');
                $param['uid'] = Token::getCurrentUid();

                $map['jobid'] = $param['jobid'];
                $map['companyid'] = $param['companyid'];
                $map['uid'] = $param['uid'];

                //  print_r($map);

                $jobsave = JobsaveModel::getJobsaveWhere($map);

                if ($jobsave) {

                    // print_r($jobsave);
                    $jobsavemodel = new JobsaveModel();
                    $jobsavemodel->delJobsave($map);

                    $data = json_encode(array('status' => 2, 'msg' => '取消收藏'));

                } else {

                    $param['createtime'] = time();

                    $jobsave = new JobsaveModel();

                    $data = $jobsave->jobSave($param);
                }


                return $data;
            }


        }


        public function myFindjob()
        {
            if (request()->isPost()) {


                $status = input('post.status');

                $Nowpage = input('post.page');
                if ($Nowpage) $Nowpage = $Nowpage; else
                    $Nowpage = 1;

                $limits = $Nowpage * 10;
                $Nowpage = 1;

                if ($status >= 0) {

                    $map['r.status'] = $status;
                }
                $uid = Token::getCurrentUid();
                $od = "r.createtime desc";
                $map['r.uid'] = $uid;

                $Jobrecord = new JobrecordModel();
                $jobrecordlist = $Jobrecord->getMyFindList($map,  $Nowpage, $limits,$od);

                $status_arr = array(0=>'新提交',1=>'同意面试',-1=>'拒绝面试',2=>'面试成功',-2=>'面试失败',3=>'录用成功',-3=>'录用失败',4=>'试用成功',-4=>'试用失败',5=>'已完成',-5=>'订单失败',6=>'已评价');

                if($jobrecordlist)
                {

                    foreach ($jobrecordlist as $k=>$v)
                    {

                        $jobrecordlist[$k]['status_str'] = $status_arr[$v['status']];
                    }
                }



                $data = array('jobrecordlist' => $jobrecordlist);
                return json_encode($data);
            }


        }


        public function mySaveJob()
        {
            if (request()->isPost()) {


                $uid = Token::getCurrentUid();
                $od = "r.createtime desc";
                $map['r.uid'] = $uid;
                $Nowpage = input('post.page');
                if ($Nowpage) $Nowpage = $Nowpage; else
                    $Nowpage = 1;

                $limits = $Nowpage * 10;
                $Nowpage = 1;

                $Jobsave = new JobsaveModel();
                $jobsavelist = $Jobsave->getMySaveList($map,  $Nowpage, $limits, $od);

                if ($jobsavelist) {

                    foreach ($jobsavelist as $k => $v) {

                        $jobsavelist[$k]['createtime'] = date('Y-m-d');
                    }


                }

                $data = array('jobsavelist' => $jobsavelist);
                return json_encode($data);
            }


        }

        public function cancleSave()
        {

            if (request()->isPost()) {


                $id = input('post.id');

                $uid = Token::getCurrentUid();
                $map['uid'] = $uid;
                $map['id'] = $id;

                $jobsaveinfo = JobsaveModel::getJobsaveWhere($map);

                if(!$jobsaveinfo)
                {

                        $data = array('status' => 1, 'msg' => '请求数据不存在');
                        return json_encode($data);

                }

                $Jobsave = new JobsaveModel();
                $flag = $Jobsave->delJobsave($map);

                if ($flag) {

                    $data = array('status' => 0, 'msg' => '取消成功');
                } else {

                    $data = array('status' => 1, 'msg' => '取消失败');
                }


                return json_encode($data);
            }

        }

        public function pubJobInit()
        {
            $city = input('post.city');

            $uid = Token::getCurrentUid();

            $jobcatelist = JobcateModel::getJobcatelist();

            $cityinfo = CityModel::getCityByName($city);

            $arealist = AreaModel::getAreaByCityid($cityinfo['id']);

            $jobpricelist = JobpriceModel::getJobpricelist();

            $map['enabled'] = 1;
            $od = 'sort desc';


            $worktypelist = WorktypeModel::getList($map, $od);


            $data = array('jobcatelist' => $jobcatelist, 'arealist' => $arealist, 'jobpricelist' => $jobpricelist, 'worktypelist' => $worktypelist);

            return json_encode($data);


        }

        public function selectJobInit()
        {
            $city = input('post.city');

            $uid = Token::getCurrentUid();

            $jobcatelist = JobcateModel::getJobcatelist();


            $jobpricelist = JobpriceModel::getJobpricelist();


            $data = array('jobcatelist' => $jobcatelist, 'jobpricelist' => $jobpricelist);

            return json_encode($data);


        }


        /*
      * 检测是否登录
      */
        public function  isLogin(){

            $ctoken = input('post.ctoken');


            $valid = Token::verifyToken($ctoken);
            if($valid)
            {
                $companyid = Token::getCurrentCid($ctoken);

                if($companyid)
                    $data = array('error' => 0, 'msg' => '正常','companyid'=>$companyid);
                else
                    $data = array('error' => 1, 'msg' => '数据异常');

            }else{

                $data = array('error' => 1, 'msg' => 'Token异常');
            }

            return $data;
        }

        public function saveJob()
        {
            if (request()->isPost()) {


                $msg = $this->isLogin();

                if($msg['error'] == 0) {


                    $param = input('post.');

                    //$param['uid'] = Token::getCurrentUid();

                    $companyid =  $param['companyid'] = $msg['companyid'];

                    $param['createtime'] = $param['updatetime'] = time();


                    $job = new JobModel();
                    $param['status'] = 1;
                    $param['toptime'] =0;

                    $data = $job->insertJob($param);

                    $CompanyRecordService = new CompanyRecordService($companyid);
                    $CompanyRecordService->jobnum = -1;

                    $CompanyRecordService->mark = '添加职位';

                    $CompanyRecordService->SetJobNumRecord();


                    return $data;

                }else{


                    return  json_encode($msg);
                }
            }


        }


        public function updateJob()
        {
            if (request()->isPost()) {

                $msg = $this->isLogin();

                if($msg['error'] == 0) {

                    $param = input('post.');

                    $companyid = $msg['companyid'];

                    $map = [];

                    $map['id'] = $param['id'];
                    $map['companyid'] = $companyid;
                    $jobinfo = JobModel::getOne($map);

                    if (!$jobinfo) {
                       return json_encode(['status'=>1,'msg'=>'请求数据不存在']);
                    }

                    $param['companyid'] = $companyid;

                    $param['updatetime'] = time();

                    $job = new JobModel();

                    $data = $job->updateJob($param);


                    return $data;

                }else{

                    return  json_encode($msg);
                }
            }


        }

public function getqrcodejob()
    {
        $id = input('get.id');

        $validate = new IDMustBePositiveInt();
        $validate->goCheck();

        $JobModel  = new JobModel();

        $map = array('id'=>$id);

        $jobinfo =  $JobModel->getJob($map);//获取职位详情

         if (!$jobinfo) {
                       return json_encode(['status'=>1,'msg'=>'请求数据不存在']);
                    }

        $AccessToken = new AccessToken();

        $qrcode = $AccessToken->getJobQrcode($id);

        $qrcode = cdnurl($qrcode ,true);

        $sysinfo = SysinitModel::getSysinfo();

        $sharebg = cdnurl($sysinfo['sharebg'] ,true);



       $data = array('qrcode'=>$qrcode,'sharebg'=>$sharebg,'status'=>0,'msg'=>'请求数据正常');



     return json_encode($data);


    }


    }
